package com.springcloud.sc.ucpv5.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springcloud.sc.comm.core.util.R;
import com.springcloud.sc.ucp.v5.api.entity.UcpVoipVoice;
import com.springcloud.sc.ucp.v5.api.entity.UcpVoipVoiceRecord;
import com.springcloud.sc.ucpv5.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ucpvoipvoice")
public class UcpVoipVoiceController {
    @Autowired
    private UcpVoipVoiceService ucpVoipVoiceService;

    @Autowired
    private UcpVoipVoiceRecordService ucpVoipVoiceRecordService;
    @Autowired
    private PublicSmsStatusService publicSmsStatusService;
    @Autowired
    private UcpVoipVoiceRecordRelationService ucpVoipVoiceRecordRelationService;
    @Autowired
    private JobUcpScheduleService jobUcpScheduleService;

    @ResponseBody
    @RequestMapping(value = "/saveVoipVoiceTask", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map saveVoipVoiceTask(@RequestBody JSONObject jsonParam){
        Map map = ucpVoipVoiceService.saveVoipVoiceTask(jsonParam);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/saveVoipVoiceTaskRecord", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map saveVoipVoiceTaskRecord(@RequestBody JSONObject jsonParam){
       return ucpVoipVoiceRecordService.saveVoipVoiceTaskRecord(jsonParam);
    }

    @ResponseBody
    @RequestMapping(value = "/getVoipVoiceList", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public List getVoipVoiceList(@RequestBody JSONObject jsonParam){
        Map map = new HashMap();
        map.put("promulgator",jsonParam.get("promulgator"));
        map.put("publishStartTime" , jsonParam.get("publishStartTime") );
        map.put("publishEndTime" , jsonParam.get("publishEndTime") );
        return  ucpVoipVoiceService.queryVoipVoiceList(map);
    }

    @ResponseBody
    @RequestMapping(value = "/getVoipVoiceRecordRelationList", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public List getVoipVoiceRecordRelationList(@RequestBody JSONObject jsonParam){
        String uuid = (String)jsonParam.get("uuid");
        String calledNumber = (String) jsonParam.get("calledNumber");
        return  ucpVoipVoiceRecordRelationService.getEntityByUUidCalledNumber(uuid , calledNumber);
    }

    @ResponseBody
    @RequestMapping(value = "/getVoipVoiceRecordList", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public List getVoipVoiceRecordList(@RequestBody JSONObject jsonParam){
        return ucpVoipVoiceRecordService.queryVoipVoiceRecordList(  jsonParam );
    }

    /**
     * 提供给独山子系统调度台短信接口
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPublicSmsStatusListApi", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public List getPublicSmsStatusListApi( @RequestBody JSONObject jsonParam ){
        Map map = new HashMap();
        map.put("uuid", jsonParam.get("uuid") );
        map.put("from" , jsonParam.get("from") );
        map.put("mobile" , jsonParam.get("mobile") );
        map.put("taskID" , jsonParam.get("taskID") );
        map.put("status" , jsonParam.get("status") );
        map.put("sendtime" , jsonParam.get("sendtime") );
        map.put("receivetime" , jsonParam.get("receivetime") );
        map.put("msgContent" , jsonParam.get("msgContent") );
        map.put("index" , jsonParam.get("index") );
        map.put("startTime" , jsonParam.get("startTime") );
        map.put("endTime" , jsonParam.get("endTime") );
        return publicSmsStatusService.getPublicSmsStatusListApi( map );
    }

    @GetMapping("/voicePage")
    public R<IPage> voicePage(Page page, UcpVoipVoice ucpVoipVoice) {
        IPage p = ucpVoipVoiceService.page(page, Wrappers.query(ucpVoipVoice));
        return R.ok( p );
    }

    @GetMapping("/voiceRecordPage")
    public R<IPage> voiceRecordPage(Page page, UcpVoipVoiceRecord ucpVoipVoiceRecord) {
        IPage p = ucpVoipVoiceRecordService.page(page, Wrappers.query(ucpVoipVoiceRecord));
        return R.ok( p );
    }

    @ResponseBody
    @RequestMapping(value = "/taskCancel", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public void taskCanel(@RequestBody JSONObject jsonParam){
        /**
         * 5. 取消任务
         * 查询参数：
         *     cdi_method,            pds_task_cancel
         * 正文JSON：
         * {
         * 	"event": "voice_pub_cancel",        --操作标识， voice_pub_cancel 表示取消任务
         * 	"uuids": [                          --任务ID，数组
         * 		"string",
         * 		......
         * 		],
         * 	"time": "string",                   --取消时间
         * 	"dispatcher": "string",             --取消者，与发布者相同
         * }
         */
        // 调用莱讯停止任务接口
        jobUcpScheduleService.saveEntityByOperData(jsonParam.toString() , "pds_task_cancel");
    }

    /**
     * 提供给独山子系统调度台媒体列表
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMediaListApi", method = RequestMethod.POST , produces = "application/json;charset=UTF-8" )
    public List getMediaListApi( @RequestBody JSONObject jsonParam ){
        Map map = new HashMap();
        map.put("number", jsonParam.get("number") );
        map.put("startTime" , jsonParam.get("startTime") );
        map.put("endTime" , jsonParam.get("endTime") );
        map.put("callRefId" , jsonParam.get("callRefId") );
        return publicSmsStatusService.getMediaListApi( map );
    }
}
